#!/bin/bash
#Written by Matthew J Vona
#This program email diagnostic data to the site operator and supervisor
trap cleanup 5 6 7 8 9 
cleanup()
{
echo "Cleaning up.."
echo "Ensuring all modem connections are dropped..."
#kill any instances of wvdial that may have been run
#killall wvdial &>/dev/null;
#kill all sjinn instances which may need to be waiting (e.g. magee ath)
killall sjinn;
echo "Done"
exit;
}

while read line; do
eval $line
done < Config/config

reported=0
tar_flag=0

clean_errors()
{
for (( i = 0 ; i < 10 ; i++ ))
        do
		echo "" > Data/errors0$i
 	done
for (( i = 10 ; i < 25 ; i++ ))
        do
		echo "" > Data/errors$i
 	done

}

while :
do
        
yesterday=`date -d yesterday "+%d"`
month=`date -d yesterday "+%m"`
hour=`date "+%H"`
if [ $hour == "00" ]
then
    tar_flag=0
fi

if [ $hour == $hour1 ] || [ $hour ==  $hour2 ] && [ $reported -eq "0" ]; then
       {
	rm -f sys_resources/.tmpcom &>/dev/null;
	echo "" >> sys_resources/.tmpcom
	echo "Site: $sitnam CARBLogger 	Reporting for `date "+%m"`/`date "+%d"`" >> sys_resources/.tmpcom
	echo "Site Operator:$siteop Site Supervisor:$sitesup " >> sys_resources/.tmpcom
	number=$(ps -fe | grep testd | grep -v grep | wc -l)
	echo "Number of Active Instruments: $number                               " >> sys_resources/.tmpcom
	echo "DATA STATE - - - - - - - - - - - - - - - - - - " >> sys_resources/.tmpcom
        size=$(ls -lah Data | grep total)
        al_cons=$(cat Data/errors[0-2][0-9] 2>/dev/null | grep : | wc -l)
        echo "Data Directory Size: $size 	Alerts Recorded: $al_cons                        #" >> sys_resources/.tmpcom
        echo " -----------------------ALERT DETAILS- - - - - - - - - - - - - - - - - - - " >> sys_resources/.tmpcom
        count_drv=1
        for i in $active_inst;
        	 do
           		 drv_list[$count_drv]="$i"
			 usr_count=$(ls -B resources/$i/.usr*)
			 for k in $usr_count
			 do
			     temp=$(echo "${k#*/*/.usr}")
			     chn_nam[$count_drv]=$(cat resources/$i/.usr$temp | grep -o "\-r [-A-Za-z0-9_]*" | awk '{ print $2 }')
                 	     count_drv=`expr $count_drv + 1`
                 	     max_count_drv="$count_drv"
			 done
                 done
        #TEST TO SEE IF ANY ERRORS FILES EXIST IF SO PREPARE THEM       	
        if (stat Data/errors* &> /dev/null); then
        	{
        	#read all errors files, sor the contents to count the numbers for each
        	cat Data/errors[0-2]* | sort >Data/.tmpstats
        	#create an errors.txt file from the sorted contents
        	cp Data/.tmpstats Data/errors.txt              
                #for each instrument tally the number of errors present
                #and write in the .tmpcom file in the sys_resources folder 
                  for (( i = 1 ; i < max_count_drv ; i++ ))
                      do
	                      num_err=$(cat Data/.tmpstats | grep ${chn_nam[$i]} | wc -l)
        	              echo "${chn_nam[$i]} has $num_err errors" >> sys_resources/.tmpcom                       
        	      done
		}
	fi
	#tes to seehow many lines exist in differences between two bam config files
	files=$(ls Data/.bamcfgdlta* 2>/dev/null | wc -l)
       if [ "$files" != "0" ]; then
       		{ 
	        if (cat Data/.bamcfg* >> Data/errors.txt ); then
       			 {
			     al_cons=`expr $al_cons + 1`
			     for (( i = 1 ; i < max_count_drv ; i++ ))
			     do
			        err=0
	                        err=$(cat Data/.bamcfg* | grep "${chn_nam[$i]} configuration" | wc -l)
			        if [ "$err" -gt 0 ] 
			        then
        	                   echo "${chn_nam[$i]} has config errors" >> sys_resources/.tmpcom              
			        fi
			     done
        		     rm -f Data/.bamcf* > /dev/null
       			 }   
       		fi
        	}
       fi
       	subject="CARBLogger: Status"
       	if [ $al_cons -gt 0 ]; then
       		{
       		subject=$subject" $sitnam Includes Errors"
       		}
       		else
        	{
      		 subject=$subject" $sitnam Nominal"
        	}
       	fi

############ADDED FUNCTION FOR SENDING VIA MODEM/MSMTP/SLOW CONNECTIONS
############CHECK FOR AN INTERNET CONNECTION#############################
#If we have a non-loopback address, we can go ahead and send out the email.
#If not, we will check to see if wvdial is installed, and if so make a call to it.
#Regardless, we will send the email, which gets stored for later transmission
#by Mutt if no IP address was obtained. Finally if we did get an IP address,
#we will set a flag to terminate wvdial after we send out.
#########################################################################
	ip_present=`ifconfig | grep -v "127.0.0.1" | grep inet\ addr | wc -l`
	if [ "$ip_present" == "0" ]; then
		{
		wvdial 1> Data/CARBLogger.log&
		sleep 30;
		recips="$siteop $sitesup";  
		if [ $scnd_email == "Y" ]; then
        		{
                		recips="$recips $sitescnd";
        		}
		fi 
		now=`date`
		echo "CARBLogger Time before NIST SYNC: $now" >> "sys_resources/.tmpcom"; 
		#grab a working ntp server
		ntp_srvr=`ntpq -p -n | grep -v === | grep -v remote | awk '($8 < 3000){ print $1 }'`
		#find the path to ntpdate
		ntp_pth=`which ntpdate | awk '{ print $1 }'`
		#sync the carblogger to ntp server
		#for i in $ntp_srvr;
		#do
		#{
			$ntp_pth -s -p 8 -u $ntp_srvr &> /dev/null ;  
		#}
		#done
		now=`date`
		echo "CARBLogger Time after NIST SYNC: $now" >> "sys_resources/.tmpcom"; 
		sleep 60;
		cat "sys_resources/.tmpcom" | mutt -s "$subject" -a "Data/errors.txt" -- $recips && sleep 60;
		#IF THE SYSTEM IS GENERATING DATA EXTRACTS, MAIL THOSE OUT
		if (stat spm[0-3][0-9]* 1> /dev/null); then
		{
		echo "Data Extracts" | mutt -s "$sitnam data" -a spm[0-3][0-9]* -a raw[0-9][0-9]* -- $recips && sleep 10
#		rm -f spm[0-3][0-9]* &> /dev/null
#		rm -f raw[0-9][0-9]* &> /dev/null
		}
		fi
		#IF THE SYSTEM IS GENERATING MIN EXTRACTS, FTP THOSE OUT
		#if (stat *.MIN &> /dev/null); then
		#{
		#	forq i in `ls *.MIN`
		#		do
		#			{
		#			bash sys_resources/carbput.sh $i &1> /dev/null
		#			sleep 30;
		#			
		#			}
		#		done
		#}
		#fi
		#commented out the following for cataline deployment
		#killall wvdial &> /dev/null
		clean_errors
		#rm-f Data/errors* &> /dev/null
		}
	else 
		{
		recips="$siteop $sitesup";  
		if [ $scnd_email == "y" ]; then
        			{
                		recips="$recips $sitescnd";
        			}
		fi 
		cat "sys_resources/.tmpcom" | mutt -s "$subject" -a "Data/errors.txt" -- $recips && sleep 10;
	    	wait;
		if (stat spm[0-3][0-9]* 1>/dev/null 2>/dev/null); then
		{
		echo "Data Extracts" | mutt -s "$sitnam data" -a spm[0-3][0-9]* -a raw[0-9][0-9]* -- $recips && sleep 30;
		sleep 60
		rm -f spm[0-3][0-9]* 1> /dev/null 2> /dev/null
		rm -f raw[0-9][0-9]* 1> /dev/null 2> /dev/null
		}
		fi
		#IF THE SYSTEM IS GENERATING MIN EXTRACTS, FTP THOSE OUT
		#if (stat *.MIN &> /dev/null); then
		#{
		#	for i in `ls *.MIN`
		#		do
		#			{
		#			bash sys_resources/carbput.sh $i
		#			}
		#		done
		#}
		#fi
		#killall wvdial &> /dev/null
		clean_errors;
		}
	fi
        clean_errors
        sleep 10;
        #If the conditions were present causing wvdial to spawn we kill wvdial now
        #HERE WE INDICATE THAT A REPORT WAS SENT OUR THIS HOUR
        reported=1;
 	#killall wvdial &> /dev/null       
       }
elif [ $hour != $hour1 ] && [ $hour != $hour2 ] ; then
	{
	#SINCE THIS IS NOT A REPORTING HOUR, WE RESET THE REPORTING FLAG HERE
	reported=0
	if [ "$hour" -eq "03" ]; then
		{
		tar czf `date -d "yesterday" "+raw%d%m%Y"`.tgz Data/`date -d "yesterday" "+${saroad}-%Y-%d-%m"`
		bash sys_resources/spmout.sh $yesterday $month `date -d yesterday "+%Y"` 1> /dev/null 2> /dev/null	
		}
	fi
		
		#CODE FIXED TO NOT REMOVE DATA PRIOR TO INGEST AND OR SUBMITTAL
		if [ $tar_flag -eq 0 ] && [ `date "+%d"` -eq "10" ]; then
		
		{	if (tar czf `date -d "3 months ago" "+%m-%Y"`.tgz Data/*`date -d "3 months ago" "+%m"` 1> /dev/null); then
				{
					rm Data/*`date -d "3 months ago" "+%m"`
					tar_flag=1
				}
			fi
		}
		fi
		
	
	sleep 3600;
}	
else
{
	sleep 60;
}
fi
done;

